/***********************************************
 @Name :        小铭同学
 @Time :        2025-08-15
 @Email :       LiaoMingWJ233@163.com
 @Comment :     自定义控件 天气预报信息类 低温曲线图
 @FileName :    weatherlowchart.cpp
 @Version :     1.0
************************************************/


#include "weatherlowchart.h"

WeatherLowChart::WeatherLowChart(QWidget *parent) : WeatherChart(parent)
{
    
}

void WeatherLowChart::calculateYRange(double &min, double &max)
{
    // 仅使用低温数据计算范围
    min = lowTemp[0];
    max = lowTemp[0];
    
    for(int i = 1; i < 7; ++i) {
        if(lowTemp[i] < min) min = lowTemp[i];
        if(lowTemp[i] > max) max = lowTemp[i];
    }
    
    // 扩大10%范围使曲线不贴边
    double range = max - min;
    // 在calculateYRange中修改：
    min -= range * 0.8; // 扩大80%范围
    max += range * 0.8;
}

void WeatherLowChart::drawTemperatureCurve(QPainter &painter, const QRect &chartRect,
                                        double minTemp, double maxTemp)
{
    painter.save();
    
    // 计算缩放比例（基于低温范围）
    double tempRange = maxTemp - minTemp;
    double yScale = chartRect.height() / tempRange;
    int dayWidth = chartRect.width() / 7;
    
    // 创建路径（使用低温数据）
    QPainterPath curvePath;
    QPainterPath fillPath;
    
    // 第一个点
    int firstX = chartRect.left() + dayWidth/2 ;
    double firstY = chartRect.bottom() - (lowTemp[0] - minTemp) * yScale;
    curvePath.moveTo(firstX, firstY);
    fillPath.moveTo(firstX, chartRect.bottom());
    fillPath.lineTo(firstX, firstY);
    
    // 中间点
    for(int i = 1; i < 7; ++i) {
        int x = chartRect.left() + i * dayWidth + dayWidth/2;
        double y = chartRect.bottom() - (lowTemp[i] - minTemp) * yScale;
        
        // 贝塞尔曲线控制点
        int prevX = chartRect.left() + (i-1) * dayWidth + dayWidth/2;
        double prevY = chartRect.bottom() - (lowTemp[i-1] - minTemp) * yScale;
        
        curvePath.cubicTo(prevX + dayWidth/3, prevY,
                         x - dayWidth/3, y,
                         x, y);
        fillPath.cubicTo(prevX + dayWidth/3, prevY,
                        x - dayWidth/3, y,
                        x, y);
    }
    
    // 填充曲线下方（浅蓝色）
    fillPath.lineTo(chartRect.left() + 6*dayWidth + dayWidth/2, chartRect.bottom());
    painter.fillPath(fillPath, QColor(200, 240, 255));
    
    // 绘制曲线（钢蓝色）
    painter.setPen(QPen(QColor(70, 130, 180), 2));
    painter.drawPath(curvePath);
    
    // 绘制温度点和标签
    QFont font = painter.font();
    font.setPixelSize(10);
    painter.setFont(font);
    
    for(int i = 0; i < 7; ++i) {
        int x = chartRect.left() + i * dayWidth + dayWidth/2;
        double y = chartRect.bottom() - (lowTemp[i] - minTemp) * yScale;
        y = qBound(chartRect.top() + 5.0, y, chartRect.bottom() - 5.0);
        
        // 绘制锚点
        painter.setBrush(QColor(70, 130, 180));
        painter.drawEllipse(QPointF(x, y), 3, 3);
        
        // 绘制温度文本（显示在点下方）
        QString tempText = QString::number(lowTemp[i]) + "°";
        QRect textRect(x - 15, y + 8, 30, 15);
        
        // 绘制温度标签
        painter.setPen(QColor(255, 69, 0));
        painter.drawText(textRect, Qt::AlignCenter, tempText);
    }
    
    painter.restore();
}

void WeatherLowChart::drawDateLabels(QPainter &painter, const QRect &dateRect)
{
    painter.save();
    
    // 设置日期文字
    painter.setPen(QColor(255, 69, 0));
    
    QFont font = painter.font();
    font.setPixelSize(10);
    painter.setFont(font);
    
    int dayWidth = width() / 7;
    for(int i = 0; i < 7; ++i) {
        int x = dayWidth * i + dayWidth/2;
        QRect textRect(x - 25, dateRect.top(), 50, dateRect.height());
        painter.drawText(textRect, Qt::AlignCenter, days[i]);
    }
    
    painter.restore();
}
